using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ruminate.GUI.Framework;
using Ruminate.Utils;

namespace Ruminate.GUI.Content {

    /// <summary>
    /// Fills the current element by filling the child 
    /// element to the parents safe area. 
    /// </summary>
    public class Fill : LocationComponent {

        #region Exposed Data

        override public Vector2 Location {
            get { return Vector2.Zero; }
            set { throw new Exception("Can not set Fill location"); }
        }
        override public Vector2 Size {
            get { return _renderSize; }
            set { throw new Exception("Can not set Fill size"); }
        }

        override public Vector2 RenderLocation { get {
            return _renderLocation;
        } }
        override public Rectangle RenderBounds { get {
            return new Rectangle(
                (int)_renderLocation.X,
                (int)_renderLocation.Y,
                (int)_renderSize.X,
                (int)_renderSize.Y);
        } }

        override public Vector2 RenderSafeAreaLocation { get {
            return _safeLocation;
        } }
        override public Rectangle RenderSafeArea { get {
            return new Rectangle(
                (int)_safeLocation.X,
                (int)_safeLocation.Y,
                (int)_safeSize.X,
                (int)_safeSize.Y);
        } }

        override public sealed Vector2 ChildOffset { get; set; }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        private Vector2 _renderLocation, _renderSize;
        private Vector2 _safeLocation, _safeSize;
        private readonly int _buffer;

        public Fill(int buffer) {

            _buffer = buffer;
            ChildOffset = Vector2.Zero;
        }

        internal protected override void AlignToViewport(GraphicsDevice graphicsDevice) {

            _renderLocation = new Vector2(_buffer, _buffer);
            _renderSize = new Vector2(
                graphicsDevice.Viewport.Width - 2 * _buffer,
                graphicsDevice.Viewport.Height - 2 * _buffer);

            var safeArea = LinkedElement.ChildRenderArea();
            _safeLocation = safeArea.Location.ToVector2() 
                + new Vector2(_buffer, _buffer);
            _safeSize = new Vector2(
                safeArea.Width - 2 * _buffer,
                safeArea.Height - 2 * _buffer);
        }

        internal protected override void AlignToParentElement() {

            _renderLocation = LinkedElement.Parent.LocationComponent.RenderSafeAreaLocation
                + new Vector2(_buffer, _buffer);
            _renderSize = new Vector2(
                LinkedElement.Parent.LocationComponent.RenderSafeArea.Width - 2 * _buffer,
                LinkedElement.Parent.LocationComponent.RenderSafeArea.Height - 2 * _buffer);

            var safeArea = LinkedElement.ChildRenderArea();
            _safeLocation = safeArea.Location.ToVector2()
                + new Vector2(_buffer, _buffer);
            _safeSize = new Vector2(
                safeArea.Width - 2 * _buffer, 
                safeArea.Height - 2 * _buffer);
        }        
    }
}
